/**
 * 
 */
package com.surent.controller.activity;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;
import android.app.ActionBar;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.SignUpCallback;
import com.hailong.surent.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.surent.model.dbmodel.User;
import com.surent.utils.SignUpValidator;
import com.surent.view.custom.toast.AppToast;

/**
 * @describe
 * @author whailong [whailong2010@gmail.com]
 * @date Sep 27, 201412:31:27 AM
 */
@ContentView(R.layout.aty_register)
public class AtyRegister extends SwipeBackActivity {
	@ViewInject(R.id.et_atyRegister_phoneNumber)
	private EditText etPhoneNum;

	@ViewInject(R.id.et_atyRegister_password)
	private EditText etPassword;
	@ViewInject(R.id.et_atyRegister_confirmpassword)
	private EditText etConfirmPw;

	@ViewInject(R.id.et_atyRegister_veriCode)
	private EditText etVeriCode;

	@ViewInject(R.id.btn_atyRegister_requestCode)
	private Button btnRequestCode;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);

		ActionBar ab = getActionBar();
		ab.setDisplayHomeAsUpEnabled(true);
		ab.setDisplayUseLogoEnabled(false);
		getSwipeBackLayout().setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
	}

	@OnClick({ R.id.btn_atyRegister_requestCode, R.id.btn_atyRegister_signup })
	public void viewOnClick(View view) {
		// if (view.getId() == R.id.btn_atyRegister_requestCode) {
		String phoneNum = etPhoneNum.getText().toString();
		String password = etPassword.getText().toString();

		String strResult = new SignUpValidator(phoneNum, password, etConfirmPw.getText().toString()).getResult();

		if (strResult != null) {
			AppToast.makeText(this, strResult, AppToast.ALERT);
			return;
		}

		User user = new User();
		user.setUsername(phoneNum);
		user.setPassword(etPassword.getText().toString());
		// user.setMobilePhoneNumber(phoneNum);
		user.signUpInBackground(new SignUpCallback() {

			@Override
			public void done(AVException e) {
				if (e != null) {
					AppToast.makeText(AtyRegister.this, e.toString(), AppToast.ALERT);
					return;
				}
				AtyRegister.this.finish();
			}
		});

		// }
		// else {
		// AVUser.verifyMobilePhoneInBackground(etVeriCode.getText().toString(),
		// new AVMobilePhoneVerifyCallback() {
		//
		// @Override
		// public void done(AVException e) {
		//
		// }
		// });
		// }
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		this.finish();
		return super.onOptionsItemSelected(item);
	}
}
